package com.navi.loaddata.loadService.impl.baseData;

import com.navi.loaddata.core.errorLog.ErrorCode;
import com.navi.loaddata.core.etlException.ETLException;
import com.navi.loaddata.core.etlException.ETLException2;
import com.navi.loaddata.core.gpEntity.MArrayParamD;
import com.navi.loaddata.core.gpEntity.MOpeD;
import com.navi.loaddata.core.gpEntity.MParamD;
import com.navi.loaddata.core.mesEntity.MesCtPospairproduct;
import com.navi.loaddata.core.mesEntity.MesProducthistory;
import com.navi.loaddata.core.mysqlEntity.EtlConfDModel;
import com.navi.loaddata.core.oracleEntity.EtlCmnGlassV;
import com.navi.loaddata.core.oracleEntity.EtlStepV;
import com.navi.loaddata.core.oracleEntity.array.EtlArrayParamV;
import com.navi.loaddata.core.oracleEntity.base.EtlParamV;
import com.navi.loaddata.core.repository.impl.MesRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jay
 */
@Service
public class ProcessBaseDataService {
    @Autowired
    private MesRepositoryImpl daomes;
    private static final Logger logger = LoggerFactory.getLogger(ProcessBaseDataService.class);

    public Map<String, MesCtPospairproduct> getMapMesCt1() throws Exception {
        Map<String, MesCtPospairproduct> mapMesCt1 = new HashMap<>();
        try {
            String hql1 = "from MesCtPospairproduct where  FACTORYNAME = 'ACF' and ACTIONTYPE = 'UNPACK'";
            List<MesCtPospairproduct> listMesCt1 = daomes.find(MesCtPospairproduct.class, hql1);
            listMesCt1.forEach(mesCt1 -> {
                mapMesCt1.put(mesCt1.getDestproductspecname(), mesCt1);
            });
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_GET_MES_HIS_ACF,e);
        }
        return mapMesCt1;
    }
    public Map<String, MesCtPospairproduct> getMapMesCt2() throws Exception {
        Map<String, MesCtPospairproduct> mapMesCt2 = new HashMap<>();
        try {
            String hql2 = "from MesCtPospairproduct where  FACTORYNAME = 'OC' and ACTIONTYPE = 'STB1'";
            List<MesCtPospairproduct> listMesCt2 = daomes.find(MesCtPospairproduct.class, hql2);
            listMesCt2.forEach(mesCt2 -> {
                mapMesCt2.put(mesCt2.getDestproductspecname(), mesCt2);
            });
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_GET_MES_HIS_OC,e);
        }
        return mapMesCt2;
    }

    //cmn glass
    public EtlCmnGlassV processCmnGlass(EtlConfDModel etlConfD, List<MesProducthistory> commonGlassVList, Map<String, MesCtPospairproduct> mapMesCt1) throws Exception {
        EtlCmnGlassV etlCmnGlassV = new EtlCmnGlassV();
        try {
            commonGlassVList.forEach(cmnGlass -> {
                switch (etlConfD.getShop()) {
                    case "ARRAY":
                        etlCmnGlassV.setGlassId(cmnGlass.getProductname());
                        etlCmnGlassV.setProductId(cmnGlass.getProductspecname());
//                etlCmnGlassV.setProductVersion(cmnGlass.getProductspecversion());
                        if (null != mapMesCt1.get(cmnGlass.getProductname()) && !StringUtils.isEmpty(mapMesCt1.get(cmnGlass.getProductname()).getDestproductspecname())) {
                            etlCmnGlassV.setBareGlassProduct(mapMesCt1.get(cmnGlass.getProductname()).getDestproductspecname());
                        }
//                etlArrayCmnGlassV.setSourceProductAry(mapMesCt2.get(cmnGlass.getProductname()));

                        break;
                    case "CF":
                        etlCmnGlassV.setGlassId(cmnGlass.getProductname());
                        etlCmnGlassV.setProductId(cmnGlass.getProductspecname());
//                    etlCfCmnGlassV.setProductVersion(cmnGlass.getProductspecversion());
                        if (null != mapMesCt1.get(cmnGlass.getProductname()) && !StringUtils.isEmpty(mapMesCt1.get(cmnGlass.getProductname()).getDestproductspecname())) {
                            etlCmnGlassV.setBareGlassProduct(mapMesCt1.get(cmnGlass.getProductname()).getDestproductspecname());
                        }
                        break;
                    case "OC":
                        if ("CELL1".equals(etlConfD.getCellType())) {
                            etlCmnGlassV.setComponentId(cmnGlass.getProductname());
                            etlCmnGlassV.setProductId(cmnGlass.getProductspecname());
//                        etlCfCmnGlassV.setProductVersion(cmnGlass.getProductspecversion());
                            if (null != mapMesCt1.get(cmnGlass.getProductname()) && !StringUtils.isEmpty(mapMesCt1.get(cmnGlass.getProductname()).getDestproductspecname())) {
                                etlCmnGlassV.setBareGlassProduct(mapMesCt1.get(cmnGlass.getProductname()).getDestproductspecname());
                            }
//                        etlCfCmnGlassV.setSourceProductAry(mapMesCt2.get(cmnGlass.getProductname()));

                        } else if ("CELL2".equals(etlConfD.getCellType())) {
                            etlCmnGlassV.setChipId(cmnGlass.getProductname());
                            etlCmnGlassV.setProductId(cmnGlass.getProductspecname());
                            etlCmnGlassV.setChipNo(cmnGlass.getProductname().substring(cmnGlass.getProductname().length() - 2, cmnGlass.getProductname().length()));
//                etlCell2CmnChipV.setProductVersion(cmnGlass.getProductspecversion());
                            if (null != mapMesCt1.get(cmnGlass.getProductname()) && !StringUtils.isEmpty(mapMesCt1.get(cmnGlass.getProductname()).getDestproductspecname())) {
                                etlCmnGlassV.setBareGlassProduct(mapMesCt1.get(cmnGlass.getProductname()).getDestproductspecname());
                            }
//                etlCell2CmnChipV.setSourceProductAry(mapMesCt2.get(cmnGlass.getProductname()));

                        }
                        break;
                }
            });
        }catch (Exception e){
            throw new ETLException2(ErrorCode.ERROR_PROCESS_GP2ORA_CMN_GLASS,e);
        }
        return etlCmnGlassV;
    }


    public List<EtlStepV> processStep(List<MOpeD> mesStep) throws Exception {
        List<EtlStepV> etlStepVList = new ArrayList<>();
            mesStep.forEach(step -> {
                if (!(step.getOpeId().length() > 5 && "D".equals(step.getOpeId().substring(5, 6)))) {
                    EtlStepV etlStepV = new EtlStepV();
                    etlStepV.setStepId(step.getOpeId());
                    etlStepV.setStepDesc("OTHER");
                    if(StringUtils.isEmpty(step.getGroupName())){
                        etlStepV.setStepGroup("OTHER");
                    }else {
                        etlStepV.setStepGroup(step.getGroupName());
                    }
                    etlStepV.setStepSeq(step.getOpeId());
                    etlStepV.setStepType("OTHER");
                    etlStepVList.add(etlStepV);
                }
            });
        return etlStepVList;
    }

    public List<EtlParamV> processParamInfo2(List<MParamD> paramList) throws Exception {
        List<EtlParamV> etlParamVList = new ArrayList<>();
        try {
            paramList.forEach(param -> {
                EtlParamV etlParamV = new EtlParamV();
                etlParamV.setParamCollection(param.getParamCollection());
                etlParamV.setParamName(param.getParamName());
                etlParamV.setParamCollectionGroup(param.getParamCollectionGroup());
                etlParamV.setParamGroup(param.getParamGroup());
                etlParamVList.add(etlParamV);
            });
        } catch (Exception e) {
            throw new ETLException2(ErrorCode.ERROR_PROCESS_GP2ORA_PARAM,e);
        }
        return etlParamVList;
    }

    public List<EtlArrayParamV> processParamInfo(List<MArrayParamD> arrayParamList, int errorProcessParamD) throws Exception {
        List<EtlArrayParamV> etlArrayParamVList = new ArrayList<>();
        try {
            arrayParamList.forEach(param -> {
                EtlArrayParamV etlArrayParamV = new EtlArrayParamV();
                etlArrayParamV.setParamCollection(param.getParamCollection());
                etlArrayParamV.setParamName(param.getParamName());
                etlArrayParamV.setParamCollectionGroup(param.getParamCollectionGroup());
                etlArrayParamV.setParamGroup(param.getParamGroup());
                etlArrayParamVList.add(etlArrayParamV);
            });
        } catch (Exception e) {
            logger.error(errorProcessParamD + ":" + e.getMessage());
            logger.error(LoggerUtil.stackTraceToString(e));
            throw new ETLException(errorProcessParamD + ":" + e.getMessage());
        }
        return etlArrayParamVList;
    }
}
